package cn.skyquant.quant4j.service.boot.web.controller;

import cn.skyquant.quant4j.api.dto.ResultDTO;
import cn.skyquant.quant4j.api.economics.EconomicsDTO;
import cn.skyquant.quant4j.api.economics.EconomicsService;
import cn.skyquant.quant4j.sdk.util.time.CalendarUtils;
import cn.skyquant.quant4j.service.boot.model.economics.jinshi.Economics;
import cn.skyquant.quant4j.service.boot.model.economics.jinshi.EconomicsRepository;
import cn.skyquant.quant4j.service.boot.web.annotation.NoVerify;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/economics")
@NoVerify
public class EconomicsController  {
    @Resource
    EconomicsRepository economicsRepository;
    @Resource
    EconomicsService economicsService;

    @RequestMapping(value = "/{thedate}",method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO<List<Economics>> list(
            @PathVariable(required = true) String thedate) {
        try {
            Date publicTime = CalendarUtils.parseOption(thedate);
            List<Economics> economicsList = economicsRepository.findByRecordTime(publicTime);
            return ResultDTO.success(economicsList);
        } catch (ParseException e) {
            return ResultDTO.error;
        }
    }


    @RequestMapping(value = "/public_time",method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO<List<EconomicsDTO>> list(
            @RequestParam(required = true) long from,
            @RequestParam(required = true) long to) {
        return economicsService.findBetweenPublicTime(from,to);
    }
}
